package com.example.tsbs.controller;

import com.example.tsbs.service.TaskService;
import com.example.tsbs.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Slf4j
@Api(tags="可执行文件测试")
@RestController
@RequestMapping("/api")
public class exeTest {

    @Autowired
    private TaskService taskService;

    @ApiOperation("频域可执行文件单线程测试")
    @PostMapping("/freqTest")
    public String freqTest() {
        String result = null;
        String exePath = "/root/tsbsDuiJie/freq_cal";
        String jsonPath = "/root/tsbsDuiJie/freq.json";
        String type = "0";
        String runNumber = "3";
        String index = "0";
        if(FileUtil.isFileExist(exePath)) {
            return exePath+"文件不存在";
        }
        if(FileUtil.isFileExist(jsonPath)) {
            return jsonPath+"文件不存在";
        }
        ProcessBuilder processBuilder = new ProcessBuilder();
//        processBuilder.command(exePath, "0", jsonPath, "3","0");
        processBuilder.command(exePath, type, jsonPath, runNumber,index);
        processBuilder.redirectErrorStream(true);
        try {
            Process process = processBuilder.start();

            // 读取标准输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // 等待程序执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
            System.out.println("Program exited with value: " + exitValue);
            result = String.valueOf(exitValue);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }

}
